package com.alogic.idnote.xscript;

import org.apache.commons.lang3.StringUtils;

import com.alogic.idnote.IdNote;
import com.alogic.idnote.IdNoteFactory;
import com.alogic.idnote.IdNoteGroup;
import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.alogic.xscript.plugins.Segment;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;

/**
 * ID解释
 * @author yyduan
 * @since 1.6.12.13 [20181213 duanyy] <br>
 */
public class IdExplain extends Segment {
	protected String $dft;
	protected String $code;
	protected String $group = "default";
	
	public IdExplain(String tag, Logiclet p) {
		super(tag, p);
	}

	@Override
	public void configure(Properties p) {
		super.configure(p);
		$dft = PropertiesConstants.getRaw(p,"dft","");
		$code = PropertiesConstants.getRaw(p,"code","");
		$group = PropertiesConstants.getRaw(p,"group",$group);
	}		
	
	@Override
	protected void onExecute(XsObject root,XsObject current, LogicletContext ctx,
			ExecuteWatcher watcher) {
		String code = PropertiesConstants.transform(ctx, $code, "");
		if (StringUtils.isNotEmpty(code)){
			String group = PropertiesConstants.transform(ctx, $group, "default");
			IdNoteGroup idnoteGroup = IdNoteFactory.get(group);
			if (idnoteGroup != null){
				IdNote note = idnoteGroup.load(code, true);
				if (note != null){
					ctx.SetValue("$note",note.getNote());
					ctx.SetValue("$advise",note.getAdvise());
					super.onExecute(root, current, ctx, watcher);
				}
			}
		}
	}
}